<script setup lang="ts">
import { hc } from "@/framework/vue/util/render.ts";
import { CopyOutlined } from "@ant-design/icons-vue";
import { computed, ref } from "vue";
import InputCopyable from "@/components/framework/input-copyable.vue";
import type { MavenArtifactInfo } from "@/modules/tools/dev/maven-search/types.ts";
import { computedAsync } from "@vueuse/core";
import { getMavenArtifactDetail } from "@/modules/tools/dev/maven-search/maven-search.service.ts";
import { copyText } from "@/util/clipboard.ts";
import { openExternal } from "@/util/shell.ts";

const { artifact } = defineProps<{
  artifact?: MavenArtifactInfo;
}>();

const open = defineModel<boolean>("open");
const loading = ref(false);
const detail = computedAsync(() => {
  if (artifact == null) {
    return null;
  } else {
    loading.value = true;
    return getMavenArtifactDetail(artifact.id, artifact.repositoryId).finally(() => (loading.value = false));
  }
});

const mavenDep = computed(() => {
  const it = detail.value;
  if (it == null) {
    return "";
  } else {
    let dep = "<dependency>\n";
    dep += `    <groupId>${it.groupId}</groupId>\n`;
    dep += `    <artifactId>${it.artifactId}</artifactId>\n`;
    dep += `    <version>${it.version}</version>\n`;
    if (it.classifier) dep += `    <classifier>${it.classifier}</classifier>\n`;
    if (it.packaging && it.packaging !== "jar") dep += `    <packaging>${it.packaging}</packaging>\n`;
    dep += `</dependency>`;
    return dep;
  }
});

const gradleDep = computed(() => {
  const it = detail.value;
  return it == null ? "" : `implementation '${it.groupId}:${it.artifactId}:${it.version}'`;
});
</script>

<template>
  <a-modal v-model:open="open" centered title="文件详情" width="800px">
    <a-spin :spinning="loading">
      <a-form :label-col="{ span: 3 }" class="pv-10">
        <a-form-item label="文件名称" class="mb-10">
          <span>{{ detail?.fileName }}</span>
        </a-form-item>
        <a-form-item label="文件大小" class="mb-10">
          <span>{{ detail?.contentLength }}</span>
        </a-form-item>
        <a-form-item label="最后修改时间" class="mb-10">
          <span>{{ detail?.lastModified }}</span>
        </a-form-item>
        <a-form-item label="下载地址" class="mb-10">
          <div class="border border-radius p-5 select-text">
            <a @click="detail?.downloadUrl && openExternal(detail?.downloadUrl)">{{ detail?.downloadUrl }}</a>
          </div>
          <template #extra>
            <div class="flex aic col-gap-10 mt-5">
              <a-tooltip title="复制连接" :mouse-leave-delay="0">
                <a-button :icon="hc(CopyOutlined)" type="text" shape="circle" @click="copyText(detail?.downloadUrl)" />
              </a-tooltip>
              <span class="color-text-tertiary">本链接在1小时内有效</span>
            </div>
          </template>
        </a-form-item>

        <a-form-item label="Maven依赖" class="mb-10">
          <div class="border border-radius p-5 position-relative">
            <pre class="m0 select-text" style="font-family: inherit">{{ mavenDep }}</pre>
            <div class="position-absolute right-0 top-0 pr-10 pt-10" v-if="mavenDep">
              <a-button :icon="hc(CopyOutlined)" type="text" shape="circle" @click="copyText(mavenDep)" />
            </div>
          </div>
        </a-form-item>
        <a-form-item label="Gradle依赖" class="mb-10">
          <input-copyable :value="gradleDep" readonly />
        </a-form-item>
      </a-form>
    </a-spin>

    <template #footer>
      <a-button type="primary" @click="open = false">关闭</a-button>
    </template>
  </a-modal>
</template>

<style scoped lang="less"></style>
